﻿using System;
using wojilu;
using wojilu.Apps.Qycms.Domain;
using wojilu.Web.Mvc;

namespace wojilu.Web.Controller.Htmls
{
    public class ListMaker : HtmlMaker
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(ListMaker));

        protected override string GetDir(String lng)
        {
            return strUtil.Join(sys.Path.Root + YBBCommon.GetConfigStr("file_htmldir"), lng + "/");
        }

        public virtual String ProcessAll(String lng, Int64 tid, Int64 mid, Int32 creattype, Int32 pnumber)
        {
            CheckDir(lng);
            String htmlPath = String.Empty;
            String html = String.Empty;
            String lnkNormal = String.Empty;
            switch (lng)
            {
                case "cn":
                    lnkNormal = Link.To(new Cn.Article.ListController().Show, tid);
                    break;

                case "big":
                    lnkNormal = Link.To(new Big5.Article.ListController().Show, tid);
                    break;

                case "en":
                    lnkNormal = Link.To(new En.Article.ListController().Show, tid);
                    break;
            }
            var t = TypeList.findById(tid);
            if (t == null)
            {
                return "<b><span class=\"colorthree\">分类ID为" + tid + "</span></b> 生成失败";
            }
            if (t.dirpath.StrIsNullOrEmpty())
            {
                htmlPath = strUtil.Join(GetDir(lng), t.dirname) + "/";
            }
            else
            {
                htmlPath = strUtil.Join(GetDir(lng), t.dirpath) + "/" + t.dirname + "/";
            }
            if (mid == 8)
            {
                var dirpath = htmlPath + YBBCommon.GetConfigStr("entrance_file") + "." + YBBCommon.GetConfigStr("file_fileex");
                html = makeHtml(lnkNormal);
                FileSystemObject.WriteFile(PathHelper.Map(dirpath), html);
            }
            else
            {
                var pageSize = t.pagemax;
                if (pageSize == 0)
                {
                    var m = Model.findById(t.mid);
                    pageSize = m.pagemax;
                }
                var recordCount = 0;
                if (t.ispart == 1)
                {
                    var titlelist = new YBBCommon().GetChildStr(tid, true);
                    recordCount = Document.count(" tid in (" + titlelist + ")  or (extid like '%," + tid + ",%' or extid like '" + tid + ",%' or extid='" + tid + "' or extid like  '%," + tid + "') order by pid asc,addtime desc");
                }
                else
                {
                    recordCount = Document.count(" tid=" + tid + " or (extid like '%," + tid + ",%' or extid like '" + tid + ",%' or extid='" + tid + "' or extid like  '%," + tid + "') order by pid asc,addtime desc");
                }
                if (recordCount == 0 || t.ispart == 2)
                {
                    var dirpath = htmlPath + YBBCommon.GetConfigStr("entrance_file") + "." + YBBCommon.GetConfigStr("file_fileex");
                    html = makeHtml(lnkNormal);
                    FileSystemObject.WriteFile(PathHelper.Map(dirpath), html);
                }
                else
                {
                    makeHtmlLoopAll(lnkNormal, recordCount, pageSize, lng, t, htmlPath, pnumber);
                }
            }
            return "<b><span class=\"colorthree\">" + t.typename + "</span></b> 生成成功，路径为 " + htmlPath + "";
        }

    }
}